/**
 *
 * @author zzhijie
 * @date 2020-11-19
 */
import request from "@/config/request";
// import {Valid} from "@/decorator/Valid";

function isInvalid(value) {
  return value === null || value === undefined || value === "undefined" || value === "null";
}

/**
 * 过滤无效参数
 * @param params
 * @returns {*}
 */
function filterParams(params) {
  if (Object.prototype.toString.call(params) !== "[object Object]") {
    return params;
  }
  params = {...params};
  if (params) {
    for (let key in params) {
      if (params.hasOwnProperty(key) && isInvalid(params[key])) {
        delete params[key];
      }
    }
  }
  return params;
}

export class HttpUtil {
  // @Valid
  static get(url, params, options) {
    return HttpUtil.request({
      url: url,
      method: 'get',
      params: params,
      ...options
    });
  }

  // @Valid
  static post(url, data, params, options) {
    return HttpUtil.request({
      url: url,
      method: 'post',
      data: data,
      params: params,
      ...options
    });
  }

  static request(options) {
    if (options) {
      let {data, params} = options;
      options.params = filterParams(params);
      options.data = filterParams(data);
    }
    return request(options);
  }
}
